package com.powerunion.datacollection.persistence.pagination;

import com.powerunion.datacollection.common.AppRuntimeException;
import com.powerunion.datacollection.util.cache.ICache;
import com.powerunion.datacollection.util.cache.StrongCache;

/*
 <!-- 1-oracle;2-sqlserver;3-mysql;4-sybase;5-db2;6-PostgreSql;7-InterBase(Firebird)  -->
 */

public class PaginationFactory {
	private static ICache cache = new StrongCache();
	private static final String defaultid = "defaultId";
	private static String url = "com.beetle.framework.persistence.pagination.Pagination";
	private static final String[] dbids = { "oracle", "sqlserver", "mysql",
			"sybase", "db2", "postgresql", "firebird" };

	public static IPagination getPaginationInstance() {
		IPagination ip = (IPagination) cache.get(defaultid);
		if (ip == null) {
			//目前只先实现oracle的模式
			ip = loadInstance("com.powerunion.webManager.persistence.pagination.imp.OraclePaginationImp");
		}
		return ip;
	}

	private static IPagination loadInstance(String classname)
			throws AppRuntimeException {
		IPagination ip = null;
		try {
			Object obj = Class.forName(classname).newInstance();
			cache.put(defaultid, (IPagination) obj);
			ip = (IPagination) obj;
		} catch (Exception ex) {
			throw new AppRuntimeException(ex);
		}
		return ip;
	}

	public static IPagination getPaginationInstance(String id) {
		IPagination ip = (IPagination) cache.get(id);
		if (ip == null) {
			String classname = "com.powerunion.webManager.persistence.pagination.imp.OraclePaginationImp";
			try {
				Object obj = Class.forName(classname).newInstance();
				cache.put(id, obj);
				ip = (IPagination) obj;
			} catch (Exception ex) {
				throw new AppRuntimeException(ex);
			}
		}
		return ip;
	}

	public static IPagination getPaginationInstance(IPagination iPagination) {
		return iPagination;
	}

}
